import { _decorator, Component, Node, Prefab, instantiate, Label, EventTouch, UITransform, director } from 'cc';
const { ccclass, property } = _decorator;

import { GameConfig } from './config/GameConfig';
import { GameRoot } from './GameRoot';
import { TaKuSDK } from './TaKuSDK';
import { ENV } from './config/main';

@ccclass('Game')
export class Game extends Component {

    @property(Prefab)
    private food1Prefab: Prefab = null;

    @property(Prefab)
    private food2Prefab: Prefab = null;

    @property(Node)
    private food1Container: Node = null;

    @property(Node)
    private food2Container: Node = null;

    @property(Node)
    private answerContainer: Node = null;

    @property(Label)
    private titleLabel: Label = null;


    start() {
        
        this.answerContainer.children.forEach(child => {
            // 添加点击事件
            child.on(Node.EventType.TOUCH_START, this.onAnswerClick, this);
        });


    }

    onAnswerClick(event: EventTouch, customEventData: string) {
        const target = event.target as Node;
        const answer = target.children[0].getComponent(Label).string;
        const levelSetting = GameConfig.getLevelSetting(GameConfig.CURRENT_LEVEL);

        // 判断name是否在levelSetting.answers中
        if (levelSetting.rightAnswer == parseInt(answer)) {
            this.node.parent.getComponent(GameRoot).onShowLayer("success");
        }else{
            this.node.parent.getComponent(GameRoot).onShowLayer("fail");
        }
    }


    renderLevel(){
        const levelSetting = GameConfig.getLevelSetting(GameConfig.CURRENT_LEVEL);

        // 清除食物
        this.food1Container.children.forEach(child => {
            child.destroy();
        });
        this.food2Container.children.forEach(child => {
            child.destroy();
        });

        // levelSetting food1 和 food2 生成对应数量的food
        for (let i = 0; i < levelSetting.food1; i++) {
            const food1 = instantiate(this.food1Prefab);
            this.food1Container.addChild(food1);
        }
        for (let i = 0; i < levelSetting.food2; i++) {
            const food2 = instantiate(this.food2Prefab);
            this.food2Container.addChild(food2);
        }

        // 修改titleLabel 文字内容
        this.titleLabel.string = levelSetting.level.toString();

        // 修改answerContainer 子节点文字内容
        this.answerContainer.children.forEach((child, index) => {
            child.children[0].getComponent(Label).string = levelSetting.answers[index].toString();
        });
     
    }

    nextLevel(){
        GameConfig.CURRENT_LEVEL++;
        this.renderLevel();

        // 只在广告开启且随机条件满足时显示插播广告
        if (ENV.AD_OPEN) {
            // 显示插播广告 随机显示
            const random = Math.random();
            if (ENV.INTERSTITIAL_OPEN && random < 0.2) {
                // 如果显示了插播广告，120秒内不显示插播广告
                const lastShowTime = localStorage.getItem("lastShowInterstitialTime");
                if (lastShowTime) {
                    const now = new Date().getTime();
                    if (now - parseInt(lastShowTime) < 120000) {
                        return;
                    }
                }
                // 更新lastShowInterstitialTime
                const now = new Date().getTime();
                localStorage.setItem("lastShowInterstitialTime", now.toString());
                TaKuSDK.getInstance().showInterstitialAd();
            }
        }
    }
}


